#include "keystone/keystone.h"
#include "windows.h"
typedef ks_err(*ptr_ks_open)(ks_arch arch, int mode, ks_engine **ks);
typedef int(*ptr_ks_asm)(ks_engine *ks, const char *string, uint64_t address, unsigned char **encoding, size_t *encoding_size, size_t *stat_count);
typedef void(*ptr_ks_free)(unsigned char *p);
typedef ks_err(*ptr_ks_close)(ks_engine *ks);

//#define CODE "INC ecx; DEC edx"
#define CODE "mov  ebp, esp"
int main(int argc, char **argv)
{
	ks_engine *ks;
	ks_err err;
	size_t count;
	unsigned char *encode;
	size_t size;


	HMODULE hDll = LoadLibrary("keystone.dll");
	if (hDll != NULL)
	{
		ptr_ks_open ksopen = (ptr_ks_open)GetProcAddress(hDll, "ks_open");
		ptr_ks_asm  ksasm = (ptr_ks_asm)GetProcAddress(hDll, "ks_asm");
		ptr_ks_free ksfree = (ptr_ks_free)GetProcAddress(hDll, "ks_free");
		ptr_ks_close ksclose = (ptr_ks_close)GetProcAddress(hDll, "ks_close");
		err = ksopen(KS_ARCH_X86, KS_MODE_32, &ks);
		if (err != KS_ERR_OK) {
			printf("ERROR: failed on ks_open(), quit\n");
			return -1;
		}

		if (ksasm(ks, CODE, 0, &encode, &size, &count) != KS_ERR_OK)
		{
			/*	printf("ERROR: ks_asm() failed & count = %lu, error = %u\n",
			count, ks_errno(ks));*/
		}
		else
		{
			size_t i;

			printf("%s = ", CODE);
			for (i = 0; i < size; i++) {
				printf("%02x ", encode[i]);
			}
			printf("\n");
			printf("Compiled: %lu bytes, statements: %lu\n", size, count);
		}
		ksfree(encode);
		ksclose(ks);
	}
	return 0;
}